﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI.Utils;

namespace Yoozoo.Gameplay.Liberty.AI
{

    public class LibertyTrafficManager : MonoBehaviour
    {
        private Dictionary<int, TrafficNpcTriggerContainer> _trafficNpcTriggers = new Dictionary<int, TrafficNpcTriggerContainer>();

        [SerializeField]
        private LibertyPathManager _pathManager;
        public LibertyPathManager PathManager => _pathManager;
        
        private void Awake()
        {
            TrafficLightController.onEnableTrafficLight += OnAddTrafficLight;
            TrafficNpcTrigger.onEnableNpcTrigger += OnAddNpcTrigger;

            AIGraphUtils.TrafficManager = this;
            LibertyAIUtils.TrafficManager = this;
        }

        public void Preload(LibertyTrafficData trafficData, Action callBack)
        {
            PathManager.Preload(trafficData, () =>
            {
                callBack?.Invoke();
            });
        }
        
        public void Dispose()
        {
            TrafficLightController.onEnableTrafficLight -= OnAddTrafficLight;
            TrafficNpcTrigger.onEnableNpcTrigger -= OnAddNpcTrigger;
            PathManager.Dispose();
        }

        // TODO 有需求再填充
        private void OnAddTrafficLight(TrafficLightController lightController)
        {
            

        }

        private void OnAddNpcTrigger(TrafficNpcTrigger npcTrigger)
        {
            TrafficNpcTriggerContainer npcTriggerContainer = new TrafficNpcTriggerContainer();
            npcTriggerContainer.npcTrigger = npcTrigger;
            npcTriggerContainer.waitPoints = new List<WaitPointContainer>(6);

            int idx = GetNpcTriggerIndex(npcTrigger.pathPoint.WayPath.WayID,npcTrigger.pathPoint.Index);
            
            for (int i = 0; i < npcTrigger.waitPoints.Count; i++)
            {
                var waitPoint = npcTrigger.waitPoints[i];
                
                WaitPointContainer waitPointContainer = new WaitPointContainer();
                waitPointContainer.index = i;
                waitPointContainer.position = waitPoint.transform.position;
                waitPointContainer.forward = waitPoint.transform.forward;

                waitPointContainer.pathId = npcTrigger.pathPoint.WayPath.WayID;
                waitPointContainer.pointIndex = npcTrigger.pathPoint.Index;

                waitPointContainer.empty = true;
                
                npcTriggerContainer.waitPoints.Add(waitPointContainer);
            }

            _trafficNpcTriggers[idx] = npcTriggerContainer;
        }
        
        public TrafficNpcTriggerContainer GetTrafficNpcTrigger(int pathId, int pointIndex)
        {
            int idx = GetNpcTriggerIndex(pathId, pointIndex);
            if (_trafficNpcTriggers.TryGetValue(idx,out var npcTriggerContainer))
            {
                return npcTriggerContainer;
            }
            return null;
        }
        
        public TrafficLightState GetNpcTriggerState(int pathId, int pointIndex)
        {
            int idx = GetNpcTriggerIndex(pathId, pointIndex);
            if (_trafficNpcTriggers.TryGetValue(idx,out var npcTriggerContainer))
            {
                return npcTriggerContainer.LightState;
            }
            return TrafficLightState.Red;
        }
        


        private int GetNpcTriggerIndex(int pathId, int pointIndex)
        {
            return pathId * 1000000 + pointIndex;
        }
        
        

    }
}